import axios, { InternalAxiosRequestConfig } from 'axios'

// 创建axios实例
const api = axios.create({
  // 直接使用远程服务器URL
  baseURL: 'https://share.zqp863426526.workers.dev/api/',
  timeout: 5000
})

// 添加请求拦截器日志
api.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  console.log('请求URL:', config.url)
  console.log('请求配置:', config)
  return config
}, error => {
  return Promise.reject(error)
})

// 请求拦截器
api.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  // 可以在这里添加认证信息
  // config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截器
api.interceptors.response.use(response => {
  return response.data
}, error => {
  console.error('API请求错误:', error)
  return Promise.reject(error)
})

// 文本上传API
export const uploadTextApi = (content: string) => {
  return api.post('/text/upload', {
    content
  })
}

// 文件上传API
export const uploadFileApi = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)

  return api.post('/file/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取文本上传列表API
export const getTextUploadsApi = () => {
  return api.get('/text/list').then(res => {
    return JSON.stringify(res)
  })
}

// 删除指定文本上传项API
export const deleteTextUploadApi = (id: number) => {
  return api.delete(`/text/delete?id=${id}`)

}